package com.dream.sys.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dream.commons.result.PageInfo;
import com.dream.commons.utils.JsonUtils;
import com.dream.sys.base.BaseController;
import com.dream.sys.model.Role;
import com.dream.sys.model.DTO.LayuiTreeDto;
import com.dream.sys.model.DTO.roleDto.RoleDto;
import com.dream.sys.model.DTO.roleDto.RoleListReqDto;
import com.dream.sys.service.ResourceService;
import com.dream.sys.service.RoleService;



/**
 * 
 * @description：角色管理
 * @author zhouy
 * @date 2018-2-14
 */

@Controller
@RequestMapping("/role")
public class RoleController  extends BaseController {


 @Autowired
 private RoleService roleService;
 @Autowired
 private ResourceService resourceService;
	  
    /**
     * 角色列表页面
     * @return
     */
    
    @GetMapping("/manager")
    public String manager() {
        return "admin/roleList";
    }
    
   
    
     /**
      * 
      * @param sysLogReqDto
      * @return
      */
    @GetMapping("/dataGrid")
    @ResponseBody
    public PageInfo<Role> dataGrid(RoleListReqDto roleListReqDto) {     
    	PageInfo<Role> page =  roleService.selectDataGrid(roleListReqDto);
        return page;
    }
    
    /**
     * 删除log
     * @param
     * @return
     */
    @PostMapping(value = "del")
    @ResponseBody
    public Object del(String[] ids){
    	String msg="删除成功";
    	roleService.deleteRoles(ids);
        return renderSuccess(msg);
    }
    
    /**
     * 新增角色页面
     * @return
     */
    @GetMapping("/addRolePage")
    public String addUser(Model model) {
    	String id = null;
    	List<LayuiTreeDto> resTree = resourceService.selectLayuiTree(id);
    	String jsonString = JsonUtils.toJson(resTree);
    	model.addAttribute("resTree",jsonString);
        return "admin/addRole";
    }
    
    /**
     * 添加角色
     *
     * @param roleDto
     * @return
     */
    @PostMapping("/addRole")
    @ResponseBody
    public Object addUser(@RequestBody RoleDto roleDto) {
    	roleService.insertByDto(roleDto);
    	return renderSuccess("添加成功");
    }
    
    /**
     * 修改角色
     *
     * @param roleDto
     * @return
     */
    @GetMapping("/updateRolePage")
    public Object updateRolePage(Model model,String id) {
    	List<LayuiTreeDto> resTree = resourceService.selectLayuiTree(id);
    	String jsonString = JsonUtils.toJson(resTree);
    	model.addAttribute("resTree",jsonString);  	
    	Role role=  roleService.selectById(id);
    	model.addAttribute("role",role);
    	return "admin/updateRole";
    }
    
    /**
     * 修改角色
     *
     * @param roleDto
     * @return
     */
    @PostMapping("/updateRole")
    @ResponseBody
    public Object updateRole(@RequestBody RoleDto roleDto) {
    	roleService.updateRole(roleDto);
    	return renderSuccess("修改成功");
    }
}
